package adapter;

import java.util.ArrayList;

import Util.InputFilterMinMax;
import Util.Util;
import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.thanhnx.tlg1.MyApp;
import com.thanhnx.tlg1.R;

public class ColAAAdapter extends BaseAdapter {
	private int item_h_100;

	private int item_h;
	private int item_h_unit;// 5 dips
	private int MAX;
	private int MIN;

	private int cur_forcus = 0;
	private MyApp myApp;
	private final Context context;
	private int[] list;

	private LayoutInflater mInflater;

	public ColAAAdapter(Context context, int[] list) {
		this.context = context;
		this.list = list;
		myApp = (MyApp) context.getApplicationContext();
		this.item_h = Util.convertDpToPixel(20, context);
		this.item_h_unit = Util.convertDpToPixel(5, context);
		this.MIN = 3 * this.item_h_unit;// 15dip
		this.MAX = 16 * this.item_h_unit;// 80dip
		this.item_h_100 = this.item_h;

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void requestZoom(int progress) {
		float x = (float) (progress / 100.0);
		item_h = (int) (x * item_h_100);
		Log.e("requestZoom(int progress)", "progress = " + progress
				+ " item_h = " + item_h);
		notifyDataSetChanged();
	}

	public int getCount() {
		return list.length;
	}

	public Object getItem(int position) {
		return list[position];
	}

	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		LinearLayout ll_root;
		EditText et;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.e("test", "getview at " + position + " value = " + list[position]);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_col_a, null);
			holder.et = (EditText) convertView.findViewById(R.id.item_col_a);
			holder.ll_root = (LinearLayout) convertView
					.findViewById(R.id.ll_root_col_a);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// Fill EditText with the value you have in data source
		// holder.ll_root.setLayoutParams(new AbsListView.LayoutParams(
		// LayoutParams.MATCH_PARENT, item_h));
		// ViewGroup.LayoutParams params = holder.et.getLayoutParams();
		// params.height = item_h;
		// holder.et.setLayoutParams(params);
		
		//TODO How to zoom the view without set height of edittext or linearlayout(Because of these ways slow the listview scroll)
		holder.et.setHeight(item_h);
		if (list[position] >= 0) {
			holder.et.setText(list[position] + "");
		} else {
			holder.et.setText("");
		}
		holder.et.setId(position);
		holder.et.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				final int posi = v.getId();
				if (!hasFocus) {
					final EditText et = (EditText) v;
					String str = et.getText().toString();
					if (!TextUtils.isEmpty(str)) {
						if ((posi - 1) >= 0 && (list[posi - 1] == -1)) {
							et.setText("");
							Toast.makeText(context,
									"You have to fill previous blank cell",
									Toast.LENGTH_SHORT).show();
						} else {
							list[posi] = Integer.parseInt(str);
							myApp.updateAt(posi);
							Log.e("focus change at position = " + posi,
									"value =  " + str);
							cur_forcus = posi + 1;
						}
					} else {
					}
				} else {
					Log.e("Start focus ", "start at " + posi);
				}
			}
		});

		// we need to update adapter once we finish with editing
		holder.et
				.setFilters(new InputFilter[] { new InputFilterMinMax(0, 36) });
		return convertView;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		// String string = "";
		// for (int i = 0; i < 30; i++) {
		// string = string + list[i] + "|";
		// }
		// Log.e("result = ", "string = " + string);
	}

	public int getItemWidth() {
		return item_h;
	}
}
